
package org.tools;

import java.awt.Polygon;
import java.awt.Rectangle;
import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;





// Referenced classes of package org.tools:
//			RuleUtil

public class PolygonAreaJudge
{

	private Polygon polygon;
	private int rate;
	private double maxX;
	private double minX;
	private double maxY;
	private double minY;

	public PolygonAreaJudge(String axisStr)
	{
		rate = 0xf4240;
		maxX = 0.0D;
		minX = 0.0D;
		maxY = 0.0D;
		minY = 0.0D;
		rate = RuleUtil.rate;
		createPolygon(axisStr);
	}

	private boolean createPolygon(String axisStr)
	{
		if (StringUtils.isEmpty(axisStr))
			return false;
		polygon = new Polygon();
		String axisInfo[] = axisStr.split("\\;");
		String currAxis = "";
		for (int cnt = 0; cnt < axisInfo.length; cnt++)
		{
			currAxis = axisInfo[cnt];
			if (StringUtils.isEmpty(currAxis))
				continue;
			int pos = currAxis.indexOf(",");
			if (pos == -1)
				continue;
			int x;
			int y;
			try
			{
				DecimalFormat decimalFormat = new DecimalFormat("#########");
				double dX = Double.parseDouble(axisInfo[cnt].substring(0, pos).trim()) * (double)rate;
				String sX = decimalFormat.format(dX);
				if (sX.indexOf(".") != -1)
					sX = sX.substring(0, sX.indexOf("."));
				x = Integer.parseInt(sX);
				double dY = Double.parseDouble(axisInfo[cnt].substring(pos + 1, currAxis.length()).trim()) * (double)rate;
				String sY = decimalFormat.format(dY);
				if (sY.indexOf(".") != -1)
					sY = sY.substring(0, sY.indexOf("."));
				y = Integer.parseInt(sY);
			}
			catch (NumberFormatException e)
			{
				continue;
			}
			polygon.addPoint(x, y);
		}

		return true;
	}

	private boolean creatBounds()
	{
		Rectangle rectangle = polygon.getBounds();
		maxX = rectangle.getMaxX();
		maxY = rectangle.getMaxY();
		minX = rectangle.getMinX();
		minY = rectangle.getMinY();
		return true;
	}

	public boolean checkIsInArea(int x, int y)
	{
		if (polygon == null)
			return false;
		else
			return polygon.contains(x, y);
	}

	public boolean isInArea(float x, float y)
	{
		int iX = (int)(x * (float)rate);
		int iY = (int)(y * (float)rate);
		return checkIsInArea(iX, iY);
	}

	public boolean isInArea(String x, String y)
	{
		if (polygon == null)
			return false;
		if (!isInAreaBounds(x, y))
		{
			return false;
		} else
		{
			int iX = RuleUtil.toInt(x);
			int iY = RuleUtil.toInt(y);
			return checkIsInArea(iX, iY);
		}
	}

	public boolean isInAreaBounds(String x, String y)
	{
		double ix = Double.parseDouble(x);
		double iy = Double.parseDouble(y);
		return ix >= minX && ix <= maxX && iy >= minY && iy <= maxY;
	}

	public static void main(String args[])
	{
		PolygonAreaJudge poly = new PolygonAreaJudge("118.126592,30.130026;118.213014,30.129031;118.2391,30.08164;118.170469,30.080835;118.112945,30.087563;118.102403,30.108645");
		System.out.println("IsInArea is "+poly.isInArea(118.1125F, 30.11524F));
		System.out.println("IsInArea is "+poly.isInArea("118.112511", "30.115244"));
		System.out.println("IsInArea is "+poly.isInArea(118.1266F, 30.13003F));
		poly = new PolygonAreaJudge("0,0;0,5;2,8;5,5;5,0");
		System.out.println("IsInArea is "+poly.isInArea(2.0F, 3F));
		Polygon p = new Polygon();
		p.addPoint(1, 2);
		p.addPoint(3, 4);
		Rectangle rec = p.getBounds();
		
		System.out.println("x : "+rec.getX());
		System.out.println("y : "+rec.getY());
		System.out.println("width : "+rec.getWidth());
		System.out.println("height : "+rec.getHeight());
		rec.grow(0, 10);
		
		System.out.println("x : "+rec.getX());
		System.out.println("y : "+rec.getY());
		System.out.println("width : "+rec.getWidth());
		System.out.println("height : "+rec.getHeight());
		
		
		poly = new PolygonAreaJudge("116.30156993865967,39.9856041788232;116.30247116088867,39.97899457895125;116.31049633026123,39.97909323439595;116.30985260009765,39.98613028865195");
		System.out.println("IsInArea is "+poly.isInArea("116.296967", "39.980400"));
	}
}
